package com.example.qmjy.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.qmjy.R;
import com.example.qmjy.fragment.ActivityPlanFragment;
import com.example.qmjy.fragment.BookFragment;
import com.example.qmjy.fragment.MeFragment;
import com.example.qmjy.fragment.YejyFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private List<Fragment> fragmentList = new ArrayList<>();

    private RadioGroup menuRg;

    private int currentTab = -1;

    private FragmentManager fm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 需要加载对应的xml
        setContentView(R.layout.activity_main);
        initView();
        initFragment();
        initEvent();
    }

    // 在单选按钮切换的事件回到哦中实现Fragment切换的逻辑
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // 开启fragment操作事务
        FragmentTransaction ft = fm.beginTransaction();
        int radioIndex = group.indexOfChild(group.findViewById(checkedId));
        Fragment fragment = fragmentList.get(radioIndex);
        if (!fragment.isAdded()) {
            ft.add(R.id.fragment_container, fragment, null);
        }

        // 如果currentTab和单选按钮的索引不一致，则隐藏之前的Fragment并显示最新的Fragment，然后设置currentTab为最新
        if (currentTab != radioIndex) {
            if (currentTab != -1) {
                // 隐藏之前的Fragment
                ft.hide(fragmentList.get(currentTab));
            }
            // 显示最新的Fragment
            ft.show(fragment);
            // 更新currentTab
            currentTab = radioIndex;
        }
        ft.commit();
    }

    // 初始化视图
    private void initView() {
        View includeView = findViewById(R.id.app_menu);
        if (includeView != null) {
            menuRg = (RadioGroup) includeView;
        } else {
            Log.e("MainActivity", "includeView is null");
        }
    }

    // 初始化事件
    private void initEvent() {
        if (menuRg != null && menuRg.getChildCount() > 0) {
            menuRg.setOnCheckedChangeListener(this);

            // 这句会触发 onCheckedChanged()，要保证 initFragment() 先执行过
            View firstChild = menuRg.getChildAt(0);
            if (firstChild instanceof RadioButton) {
                ((RadioButton) firstChild).setChecked(true);
            }
        } else {
            Log.e("MainActivity", "menuRg is null or has no children");
        }
    }


    // 初始化所有Fragment
    private void initFragment() {
        fragmentList.add(new YejyFragment());
        fragmentList.add(new BookFragment());
        fragmentList.add(new ActivityPlanFragment());
        fragmentList.add(new MeFragment());
        fm = getSupportFragmentManager();
    }
}